package com.ecjtu.stadiumre_servations.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ecjtu.stadiumre_servations.entity.domain.Venue;
import com.ecjtu.stadiumre_servations.entity.dto.VenueDTO;
import com.ecjtu.stadiumre_servations.response.Result;
import com.ecjtu.stadiumre_servations.service.VenueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalTime;


@RestController
@RequestMapping("/venue")
//@CrossOrigin
public class VenueController {
    @Autowired
    private VenueService venueService;
    @GetMapping("/findAll")
    public Result findAll(Page<Venue> page){
        try {
            return venueService.findAll(page);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    //    @GetMapping("/findByType/{venueType}")
//    public Result findByType(Page<Venue> page,@PathVariable  String venueType){
//        try {
//            return venueService.findByType(page,venueType);
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//    }
    @GetMapping("/findByType/{venueType}")
    public Result findByType(Page<Venue> page,@PathVariable  String venueType){
        try {
            return venueService.findByType(page,venueType);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable  String id){
        try {
            return venueService.findById(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @RequestMapping("/findByName/{venueName}")
    public Result findByName(Page<Venue> page,@PathVariable String venueName){
        try {
            return venueService.findByName(page,venueName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/addVenue")
    public Result addVenue(
            @RequestParam(value = "file", required = false) MultipartFile imageFile,
            @RequestParam String venueName,
            @RequestParam String venueType,
            @RequestParam String venueNumber,
            @RequestParam String venueStatus,
            @RequestParam String venueStartTime,
            @RequestParam String venueEndTime,
            @RequestParam Double venuePrice,
            @RequestParam(required = false) String description,
            @RequestParam(required = false) String venueAddress
    ) {
        // 组装DTO
        VenueDTO venueDTO = new VenueDTO();
        venueDTO.setVenueName(venueName);
        venueDTO.setVenueType(venueType);
        venueDTO.setVenueNumber(venueNumber);
        venueDTO.setVenueStatus(venueStatus);
        venueDTO.setVenueStartTime(LocalTime.parse(venueStartTime));
        venueDTO.setVenueEndTime(LocalTime.parse(venueEndTime));
        venueDTO.setVenuePrice(venuePrice);
        venueDTO.setDescription(description);
        venueDTO.setVenueAddress(venueAddress);

        // 调用service
        return venueService.addVenue(venueDTO, imageFile);
    }

    @PostMapping("/addVenueJson")
    public Result addVenueJson(@RequestBody VenueDTO venueDTO) {
        return venueService.addVenue(venueDTO, null);
    }
    @DeleteMapping("/deleteVenue/{id}")
    public Result deleteVenue(@PathVariable String id){
        return  venueService.deleteVenue(id);
    }
    @PostMapping("/updateVenue")
    public Result updateVenue(@RequestParam String id ,@RequestBody VenueDTO venueDTO){return venueService.updateVenue(id,venueDTO);}
    @GetMapping("/findAvailable")
    public Result findAvailable(Page<Venue> page){return venueService.findAvailable(page);}
    @GetMapping("/getAvailableTimeSlots")
    public Result getAvailableTimeSlots(String venueId, String date){
        return venueService.getAvailableTimeSlots(venueId,date);
    }
}